package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@CrossOrigin
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 获取套餐列表
     * @return
     */
    @PostMapping("/getSetmeal")
    public Result list() {
        log.info("接收到获取套餐列表请求");  // 添加日志
        try {
            List<Setmeal> list = setmealService.list();
            log.info("查询到的套餐列表: {}", list);  // 添加日志
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, list);
        } catch (Exception e) {
            log.error("获取套餐列表失败: ", e);  // 修改日志格式
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    /**
     * 根据套餐id查询套餐详情
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        log.info("根据套餐id查询套餐详情");
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true, MessageConstant.GET_SETMEAL_DETAIL_SUCCESS, setmeal);
    }

}
